//
//  PhotoBrowserViewController.m
//  etionUI
//
//  Created by wangjian on 1/26/14.
//  Copyright (c) 2014 GuangZhouXuanWu. All rights reserved.
//

#import "PhotoBrowserViewController.h"
#import "HandWritingView.h"
#import "DrawingView.h"
#import "UserGuideView.h"
#import "UserGuideManager.h"

typedef NS_ENUM(NSInteger, EOperationBtnType)
{
    EOperationBtnTypeBegain = 0,
    EOperationBtnTypeRetake = EOperationBtnTypeBegain,
    EOperationBtnTypeEdit,
    EOperationBtnTypeSign,
    EOperationBtnTypeRemark,
    EOperationBtnTypeDel,
    EOperationBtnTypeCount,
};

@interface CPhotoBrowserViewController () <CPhotoBrowserViewDelegate, CHandWritingViewDeleagte, CDrawingBoardViewDelegate>


@end

@implementation CPhotoBrowserViewController
{
    CPhotoBrowserView *_photoBrowserView;
}

- (CPhotoBrowserView *)photoBrowserView
{
    if (_photoBrowserView == nil)
    {
        _photoBrowserView = [[CPhotoBrowserView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _photoBrowserView.delegate = self;
        _photoBrowserView.buttonItems = [NSArray arrayWithObjects:
            [CImageBrowserButtonItem itemWithTitle:nil image:[UIImage imageNamed:@"pb_retake_normal"] selectedImage:[UIImage imageNamed:@"pb_retake_selected"]],
            [CImageBrowserButtonItem itemWithTitle:nil image:[UIImage imageNamed:@"pb_palette_normal"] selectedImage:[UIImage imageNamed:@"pb_palette_selected"]],
            [CImageBrowserButtonItem itemWithTitle:nil image:[UIImage imageNamed:@"pb_sign_normal"] selectedImage:[UIImage imageNamed:@"pb_sign_selected"]],
            [CImageBrowserButtonItem itemWithTitle:nil image:[UIImage imageNamed:@"pb_remark_normal"] selectedImage:[UIImage imageNamed:@"pb_remark_selected"]],
            [CImageBrowserButtonItem itemWithTitle:nil image:[UIImage imageNamed:@"pb_del_normal"] selectedImage:[UIImage imageNamed:@"pb_del_selected"]], nil];
    }
    return _photoBrowserView;
}

- (void)dealloc
{
    [_photoBrowserView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)showWithAnchorView:(UIView *)anchorView imageIndex:(NSUInteger)index complete:(void (^)())complete
{
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    self.view.frame = shareWindow.bounds;
    [shareWindow addSubview:self.view];
    [self.photoBrowserView showWithSupperView:self.view anchorView:anchorView image:[self.delegate photoBrowserViewController:self cellInfoAtIndex:index].image index:index fillStyle:EImageFillStyleStretchByCenterScale complete:^{
        if ([CUserGuideManager userGuideStatusForKey:@"photoBrowserViewController"] == EUserGuideStatusNeverDisplay)
        {
            CUserGuideUnit *unitBack = [CUserGuideUnit unitWithType:EUserGuideUnitTypeCircleNode area:CGRectMake(15, 15, 34, 34) text:@"点击这里返回" image:nil];
            CUserGuideUnit *unitFunBtn = [CUserGuideUnit unitWithType:EUserGuideUnitTypeCircleNode area:CGRectMake(30, self.view.height-55, self.view.width-60, 50) text:@"以下按钮的功能分别是重拍，编辑，签名，备注，删除" image:nil];
            CUserGuideUnit *unitTapGesture = [CUserGuideUnit unitWithType:EUserGuideUnitTypeText area:self.view.bounds text:@"单击图片可以隐藏或者显示功能按钮" image:nil];
            [[CUserGuideView userGuideViewWithUnits:[NSArray arrayWithObjects:unitBack, unitFunBtn, unitTapGesture, nil] delegate:nil] show];
            [CUserGuideManager setUserGuideStatus:EUserGuideStatusHasDisplay forKey:@"photoBrowserViewController"];
        }
        complete();
    }];
}

- (void)dismissWithAnchorView:(UIView *)anchorView complete:(void (^)())complete
{
    [self.photoBrowserView dismissWithAnchorView:anchorView fillStyle:EImageFillStyleStretchByCenterScale complete:^
    {
        [self.view removeFromSuperview];
        complete();
    }];
}

#pragma mark - image broswer delegate

- (NSUInteger)numberOfImagesForImageBrowserView:(CImageBrowserView *)imageBrowserView
{
    return [self.delegate numberOfPhotoForPhotoBrowserViewController:self];
}

- (id)imageBrowserView:(CImageBrowserView *)imageBrowserView cellInfoAtIndex:(NSUInteger)index
{
    return [self.delegate photoBrowserViewController:self cellInfoAtIndex:index];
}

- (void)imageBrowserView:(CImageBrowserView *)imageBrowserView didClickButtonItemAtIndex:(NSUInteger)index
{
    switch (index)
    {
        case EOperationBtnTypeRetake:
            if ([self.delegate respondsToSelector:@selector(photoBrowserViewController:retakePhotoAtIndex:)])
            {
                [self.delegate photoBrowserViewController:self retakePhotoAtIndex:imageBrowserView.curImageIndex];
            }
            break;
        case EOperationBtnTypeEdit:
            [self editPhotoAtIndex:imageBrowserView.curImageIndex];
            break;
        case EOperationBtnTypeSign:
            [self signPhotoAtIndex:imageBrowserView.curImageIndex];
            break;
        case EOperationBtnTypeRemark:
            [self editRemarkAtIndex:imageBrowserView.curImageIndex];
            break;
        case EOperationBtnTypeDel:
            if ([self.delegate respondsToSelector:@selector(photoBrowserViewController:delPhotoAtIndex:)])
            {
                [self.delegate photoBrowserViewController:self delPhotoAtIndex:imageBrowserView.curImageIndex];
            }
            break;
    }
}

- (void)imageBrowserView:(CImageBrowserView *)imageBrowserView didMoveToImageAtIndex:(NSUInteger)index
{
    if ([self.delegate respondsToSelector:@selector(photoBrowserViewController:didMoveToPhotoAtIndex:)])
    {
        [self.delegate photoBrowserViewController:self didMoveToPhotoAtIndex:index];
    }
}

- (void)imageBrowserViewNeedGetBack:(CImageBrowserView *)imageBrowserView
{
    [self.delegate photoBrowserViewControllerNeedGetBack:self];
}

- (void)editPhotoAtIndex:(NSUInteger)index
{
    CPhotoBrowserViewCell *cell = (CPhotoBrowserViewCell *)[self.photoBrowserView cellAtIndex:index];
    [self.photoBrowserView hideButtonItemsAnimated:YES complete:^(BOOL finished){}];
    [cell setRemarkShowState:NO];
    [cell setImageZoomScale:1.0 animated:YES complete:^
    {
        CDrawingBoardView *drawingView = [[[CDrawingBoardView alloc] initWithFrame:self.view.bounds] autorelease];
        drawingView.delegate = self;
        drawingView.drawingFrame = [cell frameOfImage];
        [self.view addSubview:drawingView];
    }];
}

- (void)signPhotoAtIndex:(NSUInteger)index
{
    CPhotoBrowserViewCell *cell = (CPhotoBrowserViewCell *)[self.photoBrowserView cellAtIndex:index];
    [cell setRemarkShowState:NO];
    [self.photoBrowserView hideButtonItemsAnimated:YES complete:^(BOOL finished){}];
    [cell setImageZoomScale:1.0 animated:YES complete:^
    {
        CHandWritingView *handWritingView = [[[CHandWritingView alloc] initWithFrame:self.view.bounds] autorelease];
        handWritingView.displayFrame = [cell frameOfImage];
        handWritingView.contentSize = cell.image.size;
        handWritingView.delegate = self;
        [self.view addSubview:handWritingView];
    }];
}

- (void)editRemarkAtIndex:(NSUInteger)index
{
    CPhotoBrowserViewCell *cell = (CPhotoBrowserViewCell *)[self.photoBrowserView cellAtIndex:index];
//    [self.photoBrowserView hideButtonItemsAnimated:YES complete:^(BOOL finished)
//    {
//        
//    }];
    [cell editRemark];
}

- (void)handWritingViewCancelEdit:(CHandWritingView *)view
{
    view.userInteractionEnabled = NO;
    CPhotoBrowserViewCell *cell = (CPhotoBrowserViewCell *)[self.photoBrowserView cellAtIndex:self.photoBrowserView.curImageIndex];
    [cell setRemarkShowState:YES];
    [view hideButtonsAnimated:YES complete:^(BOOL finished) {
        [view removeFromSuperview];
    }];
    [self.photoBrowserView showButtonItemsAnimated:YES complete:^(BOOL finished) {
        
    }];
}

- (void)handWritingView:(CHandWritingView *)view finishEditWithContent:(UIImage *)content
{
    CPhotoBrowserViewCell *cell = (CPhotoBrowserViewCell *)[self.photoBrowserView cellAtIndex:self.photoBrowserView.curImageIndex];
    UIGraphicsBeginImageContext(cell.image.size);
    [cell.image drawAtPoint:CGPointZero];
    [content drawAtPoint:CGPointZero];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self.delegate photoBrowserViewController:self updatePhotoAtIndex:self.photoBrowserView.curImageIndex withNewPhoto:newImage];
    [self.photoBrowserView reloadImageAtIndex:self.photoBrowserView.curImageIndex];
    
    [self handWritingViewCancelEdit:view];
}

- (void)drawingBoardViewCancelEdit:(CDrawingBoardView *)view
{
    CPhotoBrowserViewCell *cell = (CPhotoBrowserViewCell *)[self.photoBrowserView cellAtIndex:self.photoBrowserView.curImageIndex];
    [cell setRemarkShowState:YES];
    [view hideButtonsAnimated:YES complete:^(BOOL finished) {
        [view removeFromSuperview];
    }];
    [self.photoBrowserView showButtonItemsAnimated:YES complete:^(BOOL finished) {
        
    }];
}

- (void)drawingBoardViewFinishedEdit:(CDrawingBoardView *)view
{
    CPhotoBrowserViewCell *cell = (CPhotoBrowserViewCell *)[self.photoBrowserView cellAtIndex:self.photoBrowserView.curImageIndex];
    UIGraphicsBeginImageContext(cell.image.size);
    [cell.image drawAtPoint:CGPointZero];
    [view.drawingView.m_image drawInRect:CGRectMake(0, 0, cell.image.size.width, cell.image.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.delegate photoBrowserViewController:self updatePhotoAtIndex:self.photoBrowserView.curImageIndex withNewPhoto:newImage];
    [self.photoBrowserView reloadImageAtIndex:self.photoBrowserView.curImageIndex];
    
    [self drawingBoardViewCancelEdit:view];
}

- (void)photoBrowserView:(CPhotoBrowserView *)view cellAtIndex:(NSUInteger)index remarkChange:(NSString *)remark
{
    if ([self.delegate respondsToSelector:@selector(photoBrowserViewController:updateRemarkAtIndex:withNewRemark:)])
    {
        [self.delegate photoBrowserViewController:self updateRemarkAtIndex:index withNewRemark:remark];
    }
}

@end
